﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;

using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using stdole;



namespace Akiha.Core
{
    //包括标注和临时图层的处理
    public static class LableWrapper
    {
        //使用TextElment绘制标注, fieldName为要绘制的属性
        public static void AddLable(IMap pMap, ILayer layer, string fieldName)
        {
            IRgbColor pColor = new RgbColorClass()
            {
                Red = 255,
                Blue = 0,
                Green = 0
            };
            IFontDisp pFont = new StdFont()
            {
                Name = "宋体",
                Size = 5
            } as IFontDisp;
            
            ITextSymbol pTextSymbol = new TextSymbolClass()
            {
                Color = pColor,
                Font = pFont,
                Size = 11
            };

            IGraphicsContainer pGraContainer =pMap as IGraphicsContainer;

            //遍历要标注的要素
            IFeatureLayer pFeaLayer = layer as IFeatureLayer;
            IFeatureClass pFeaClass = pFeaLayer.FeatureClass;
            IFeatureCursor pFeatCur = pFeaClass.Search(null, false);
            IFeature pFeature = pFeatCur.NextFeature();
            int index = pFeature.Fields.FindField(fieldName);//要标注的字段的索引
            IEnvelope pEnv = null;
            ITextElement pTextElment = null;
            IElement pEle = null;
            while (pFeature != null)
            {
                //使用地理对象的中心作为标注的位置
                pEnv = pFeature.Extent;
                IPoint pPoint = new PointClass();
                pPoint.PutCoords(pEnv.XMin + pEnv.Width * 0.5, pEnv.YMin + pEnv.Height * 0.5);

                pTextElment = new TextElementClass()
                {
                    Symbol = pTextSymbol,
                    ScaleText = true,
                    Text = pFeature.get_Value(index).ToString()
                };
                pEle = pTextElment as IElement;
                pEle.Geometry = pPoint;
                //添加标注
                pGraContainer.AddElement(pEle, 0);
                pFeature = pFeatCur.NextFeature();
            }
           // (pMap as IActiveView).PartialRefresh(esriViewDrawPhase.esriViewGraphics, null,pMap);
        }


        //添加标注,比TextElment功能更强大
        public static void AddAnnotate(ILayer layer, string fieldName)
        {
            IGeoFeatureLayer pGeoLayer = layer as IGeoFeatureLayer;
            IAnnotateLayerPropertiesCollection IPALPColl = pGeoLayer.AnnotationProperties;
            IPALPColl.Clear();
            
            IRgbColor pColor = RgbColorWrapper.GetRGBColor(255, 255, 0,255);

            IFontDisp pFont = new StdFont()
            {
                Name = "宋体",
                Bold = true
            } as IFontDisp;

            ITextSymbol pTextSymbol = new TextSymbolClass()
            {
                Color = pColor,
                Font = pFont,
                Size = 12
            };

            //用来控制标注和要素的相对位置关系
            ILineLabelPosition pLineLpos = new LineLabelPositionClass()
            {
                Parallel = false,  //修改标注的属性
                Perpendicular = true,
                InLine = true
            };
            //用来控制标注冲突
            ILineLabelPlacementPriorities pLinePlace = new LineLabelPlacementPrioritiesClass()
            {
                AboveStart = 5, //让above 和start的优先级为5
                BelowAfter = 4
            };
            //用来实现对ILineLabelPosition 和 ILineLabelPlacementPriorities以及更高级属性的控制
            IBasicOverposterLayerProperties pBOLP = new BasicOverposterLayerPropertiesClass()
            {
                FeatureType = esriBasicOverposterFeatureType.esriOverposterPolygon,
                LineLabelPlacementPriorities = pLinePlace,
                LineLabelPosition = pLineLpos
            };

            //创建标注对象
            ILabelEngineLayerProperties pLableEngine = new LabelEngineLayerPropertiesClass()
            {
                Symbol = pTextSymbol,
                BasicOverposterLayerProperties = pBOLP,
                IsExpressionSimple = true,
                Expression = "[" + fieldName + "]"
            };

            //设置标注的参考比例尺
            IAnnotateLayerTransformationProperties pAnnoLyrPros = pLableEngine as IAnnotateLayerTransformationProperties;
            pAnnoLyrPros.ReferenceScale = 2500000;

            //设置标注可见的最大最小比例尺
            IAnnotateLayerProperties pAnnoPros = pLableEngine as IAnnotateLayerProperties;
            pAnnoPros.AnnotationMaximumScale = 2500000;
            pAnnoPros.AnnotationMinimumScale = 25000000;
            //pAnnoPros.WhereClause属性  设置过滤条件

            IPALPColl.Add(pAnnoPros);
            pGeoLayer.DisplayAnnotation = true;
        }


        //在临时图层上添加图形
        public static void AddGraphic(IMap pMap, IGeometry pGeo, ISymbol pSymbol, ISpatialReference pSpatial)
        {
            IGraphicsContainer pGraContainer = pMap as IGraphicsContainer;
            IPolygonElement pPolygonEle = new PolygonElementClass
            {
                Geometry = pGeo,
                Symbol = pSymbol as IFillSymbol,
                SpatialReference = pSpatial
            };

            pGraContainer.AddElement(pPolygonEle as IElement, 0);
        }



    }
}
